﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Strongshell.Recoil.Core.Composition.Parsing;
using Strongshell.Recoil.Core.Composition.Parsing.DependencyInjection;

namespace Strongshell.Recoil.Core.Composition.Workflow
{
    public class MappingFragment<TTarget,TProperty> : IMappingFragment<TTarget,TProperty>
    {
        private readonly FragmentContext context;
        private readonly string propertyName;

        public string PropertyName
        {
            get { return propertyName; }
        }

        public MappingFragment(FragmentContext context, string propertyName)
        {
            this.context = context;
            this.propertyName = propertyName;
        }

        public IDefinitionFragment<TTarget> To(Expression<Func<TProperty>> valueExpression)
        {
            var parser = context.ExpressionParserFactory.Create();

            parser.Parse(new NestingPropertyDependencyInjector(context.ObjectConfigurationRegistry,
                                                               context.ObjectConfiguration,
                                                               typeof (TProperty),
                                                               propertyName),
                         valueExpression.Body);

            return new DefinitionFragment<TTarget>(context);
        }
    }
}